﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 第三次作业
{
    public abstract class Shape
    {
        public abstract double getArea();
        public abstract bool isValid();
    }

    public class Triangle:Shape {
        int a, b, c;
        public Triangle(int a,int b,int c)
        {
            this.a = a; 
            this.b = b;
            this.c = c;
        }
        public override bool isValid()
        {
            return a > 0 && b > 0 && c > 0 &&
          a + b > c &&
          a + c > b &&
          b + c > a;
        }
        public override double getArea()
        {
            if (!isValid()) return 0;
            double s = (a + b + c) / 2;
            return Math.Sqrt(s * (s - a) * (s - b) * (s - c));
        }
    }
    public class Square : Shape
    {
        int side;
        public Square(int side)
        {
            this.side = side;
        }
        public override bool isValid() { return side > 0; }
        public override double getArea() { return side * side; }
    }
    public class Rectangle : Shape
    {
        int width, height;
        public Rectangle(int width, int height)
        {
            this.width = width;
            this.height = height;
        }
        public override bool isValid() { return width > 0 && height > 0 && width != height; }
        public override double getArea() { return width * height; }
    }
    public static class ShapeFactory
    {
        private static readonly Random _random = new Random();
        public static Shape CreateShape()
        {
            int type = _random.Next(0, 3);
            return type switch
            {
                0 => new Rectangle(_random.Next(1, 11), _random.Next(1, 11)),
                1 => new Square(_random.Next(1, 11)),
                2 => new Triangle(_random.Next(1, 11), _random.Next(1, 11), _random.Next(1, 11)),
                _ => throw new InvalidOperationException()
            };
        }
    }
    class Program
    {
        static void Main()
        {
            List<Shape> shapes = new List<Shape>();
            double totalArea = 0;
            for (int i = 0; i < 10; i++)
            {
                Shape shape = ShapeFactory.CreateShape();
                shapes.Add(shape);
                totalArea += shape.getArea();
            }
            Console.WriteLine($"Total area of all valid shapes: {totalArea:F2}");
        }
    }
}
